package com.weiaibenpao.demo.chislim.jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.orhanobut.logger.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/12/8.
 * 根据极光官方Demo示例以及官方论坛里的官方回复(https://community.jiguang.cn/t/topic/9218/5)极光自定义消息（或通知）的BroadcastReceiver必须
 * 静态注册才可以接收到自定义消息，无法动态注册。。。
 */

public class JPushMessageReceiver extends BroadcastReceiver {
    private static final String TAG = "JPushMessageReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Logger.e("JPushMessageReceiver:onReceive------------------->");
        Bundle bundle = intent.getExtras();
//            Logger.d(TAG+"--onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Logger.d(TAG + "JPush用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            if (bundle != null) {
                handleCustomMessage(bundle);
                Logger.d(TAG + "接受到推送下来的自定义消息:" + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            }
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Logger.d(TAG + "接受到推送下来的通知");

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Logger.d(TAG + "用户点击打开了通知");

        } else {
            Logger.d(TAG + "Unhandled intent - " + intent.getAction());
        }
    }


    private void handleCustomMessage(Bundle bundle) {
        Logger.e("handleCustomMessage:"+bundle.getString(JPushInterface.EXTRA_MESSAGE));
    }

    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
                    Logger.i(TAG, "This message has no Extra data");
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator<String> it = json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " + json.optString(myKey) + "]");
                    }
                } catch (JSONException e) {
                    Logger.e(TAG, "Get message extra JSON error!");
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }


}
